package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.service.property.domain.EstateDetail;
import com.qiaofang.applet.service.property.service.EstateAlbumPhotoService;
import com.qiaofang.applet.service.property.service.EstateDetailService;
import com.qiaofang.applet.service.property.service.EstateHouseTypeFyService;
import com.qiaofang.applet.service.property.service.EstateService;
import com.qiaofang.applet.service.property.service.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by IDEA.
 *
 * @author QF2019
 * @company qiaofang
 * @date 2020/9/12 11:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "其它：小区管理")
@RequestMapping("/api/estate")
@Slf4j
public class EstateController {

    @Autowired
    private EstateService estateService;

    @Autowired
    private EstateDetailService estateDetailService;

    @Autowired
    private EstateAlbumPhotoService estateAlbumPhotoService;

    @Autowired
    private EstateHouseTypeFyService estateHouseTypeFyService;

    @Autowired
    private UserService userService;


    @ApiOperation("小区列表")
    @GetMapping
    public ResponseEntity<Page<EstatePageDto>> list(EstatePageQueryDto estatePageQueryDto, Pageable pageable) {
        estatePageQueryDto.setCorporationCode(SecurityUtils.getCorporationCode());
        estatePageQueryDto.setCityCodes(userService.getCityCodeList(SecurityUtils.getCurrentUsername()));
        return ResponseEntity.ok(estateService.getEstatePage(estatePageQueryDto, pageable));
    }


    @ApiOperation("小区详情")
    @GetMapping("/getDetail")
    public ResponseEntity<EstateEditDto> getDetail(@RequestParam("estateUniqueUuid") String estateUniqueUuid) {
        return ResponseEntity.ok(estateDetailService.getEstateEditDetail(estateUniqueUuid));
    }

    @ApiOperation("小区详情保存")
    @PostMapping("/save")
    public ResponseEntity<Object> save(@RequestBody EstateEditDto estateEditDto) {
        estateDetailService.saveEstateDetail(estateEditDto);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @ApiOperation("查询相册类目")
    @GetMapping("/getAlbums")
    public ResponseEntity<List<EstateAlbumDto>> getAlbums(@RequestParam("estateUniqueUuid") String estateUniqueUuid) {
        return ResponseEntity.ok(estateAlbumPhotoService.getAlbums(estateUniqueUuid));
    }

    @ApiOperation("保存类目")
    @PostMapping("/saveAlbum")
    public ResponseEntity<Object> saveAlbum(@RequestBody EstateAlbumDto estateAlbumDto) {
        estateAlbumPhotoService.saveAlbum(estateAlbumDto);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("删除类目")
    @GetMapping("/deleteAlbum")
    public ResponseEntity<Object> deleteAlbum(@RequestParam("albumId") String albumId) {
        estateAlbumPhotoService.deleteAlbum(albumId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("查询相册类目图片")
    @GetMapping("/getAlbumPhotos")
    public ResponseEntity<Map<String, List<EstateAlbumPhotoDto>>> getAlbumPhotos(@RequestParam("albumId") String albumId) {
        return ResponseEntity.ok(estateAlbumPhotoService.getAlbumPhotos(albumId));
    }

    @ApiOperation("查询当前类目下可添加楼盘图片")
    @PostMapping("/getEstatePhotosToAddAlbum")
    public ResponseEntity<Map<String, List<EstatePhotoDto>>> getEstatePhotosToAddAlbum(@RequestBody EstateAlbumPhotoToAddDto estateAlbumPhotoToAddDto) {
        return ResponseEntity.ok(estateAlbumPhotoService.getAddiblePhotos(estateAlbumPhotoToAddDto));
    }

    @ApiOperation("添加相册类目图片")
    @PostMapping("/addAlbumPhotos")
    public ResponseEntity<Object> addAlbumPhotos(@RequestBody EstateAlbumPhotoAddListDto addListDto) {
        estateAlbumPhotoService.addAlbumPhotos(addListDto);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("编辑相册类目图片标签、说明")
    @PostMapping("/editAlbumPhoto")
    public ResponseEntity<Object> editAlbumPhoto(@RequestBody EstateAlbumPhotoEditDto estateAlbumPhotoEditDto) {
        estateAlbumPhotoService.editAlbumPhoto(estateAlbumPhotoEditDto);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("移除相册类目图片")
    @GetMapping("/deleteAlbumPhoto")
    public ResponseEntity<Object> deleteAlbumPhoto(@RequestParam("id") String id) {
        estateAlbumPhotoService.deleteAlbumPhoto(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("获取户型房源列表")
    @PostMapping("/getHouseTypeFy")
    public ResponseEntity<Map<String, List<EstatePropertyListDto>>> getHouseTypeFy(@RequestBody EstatePropertyQueryDto estatePropertyQueryDto) {
        return ResponseEntity.ok(estateHouseTypeFyService.getHouseTypeFyMap(estatePropertyQueryDto));
    }

    @ApiOperation("添加户型房源")
    @PostMapping("/addHouseTypeFy")
    public ResponseEntity<Object> addHouseTypeFy(@RequestBody EstatePropertyAddDto estatePropertyAddDto) {
        estateHouseTypeFyService.addHouseTypeFy(estatePropertyAddDto);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("移除户型房源")
    @PostMapping("/deleteHouseTypeFy")
    public ResponseEntity<Object> deleteHouseTypeFy(@RequestBody EstatePropertyRemoveDto estatePropertyRemoveDto) {
        estateHouseTypeFyService.deleteHouseTypeFy(estatePropertyRemoveDto);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("查询业主点评")
    @PostMapping("/listEstateComment")
    public ResponseEntity<Object> listEstateComment(@RequestBody EstateDetailsQueryDto estateDetailsQueryDto) {
        return ResponseEntity.ok(estateDetailService.listEstateComment(estateDetailsQueryDto.getEstateUniqueUuid()));
    }

    @ApiOperation("保存业主点评")
    @PostMapping("/saveEstateComment")
    public ResponseEntity<Object> saveEstateComment(@RequestBody @Valid EstateCommentAddDto estateCommentAddDto) {
        EstateDetail estateDetail = estateDetailService.getEstateDetailByUuid(estateCommentAddDto.getEstateUniqueUuid());
        //第一次操作小区编辑，为null
        if (estateDetail != null && !CollectionUtils.isEmpty(estateDetail.getEstateComments())
                && estateDetail.getEstateComments().size() > 19) {
            throw new BusinessException("最多只能添加20条点评!");
        }
        estateDetailService.addEstateComment(estateCommentAddDto);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("删除业主点评")
    @PostMapping("/deleteEstateComment")
    public ResponseEntity<Object> deleteEstateComment(@RequestBody @Valid EstateCommentDeleteDto estateCommentDeleteDto) {
        estateDetailService.deleteEstateComment(estateCommentDeleteDto);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
